package com.kun.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DeadLetterConfig {

    @Bean
    public DirectExchange deadLetterExchange() {
        return new DirectExchange("dead_letter_exchange");
    }

    @Bean
    public Queue deadLetterQueue() {
        return new Queue("dead_letter_queue", true, false, false, null);
    }

    @Bean
    public Binding bindingDeadLetter() {
        return BindingBuilder.bind(deadLetterQueue()).to(deadLetterExchange()).with("dead_letter_queue");
    }

    @Bean
    public DirectExchange deadLetterExchangeTest() {
        return new DirectExchange("dead_letter_exchange_test");
    }

    @Bean
    public Queue deadLetterQueueTest() {
        Map<String, Object> map = new HashMap<>();
        map.put("x-message-ttl",5000);   //设置消息过期时间为5秒
        map.put("x-max-length",5);      //设置队列最大长度为5
        map.put("x-dead-letter-exchange","dead_letter_exchange");
        map.put("x-dead-letter-routing-key","dead_letter_queue");
        return new Queue("dead_letter_queue_test", true, false, false, map);
    }

    @Bean
    public Binding bindingDeadLetterTest() {
        return BindingBuilder.bind(deadLetterQueueTest()).to(deadLetterExchangeTest()).with("dead_letter_queue_test");
    }



}
